//
//  ClubSettingTableViewCell.swift
//  77Club
//
//  Created by Apple on 2024/9/18.
//

import UIKit


class ClubSettingTableViewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .white
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubSettingItemCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 48.0.Scale
        tableView.layer.cornerRadius = 8.0.Scale
        tableView.layer.masksToBounds = true
        return tableView
    }()

    var models: [ClubSettingModel]?{
        didSet{
            tableView.reloadData()
        }
    }
    var yuyueGameBlock: ((_ gameId: String) -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            tableView
        }
        
        tableView.leading(12.0.Scale).trailing(12.0.Scale).top(0).bottom(12.0.Scale)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return models?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubSettingItemCell", for: indexPath) as! ClubSettingItemCell
        cell.model = models?[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        guard let md = models?[indexPath.row] else { return }
        switch md {
        case .normal:
            
            UIApplication.topController()?.navigationController?.pushViewController(ClubNormalSettingViewController(), animated: true)
            break
        case .privacy:
            
            UIApplication.topController()?.navigationController?.pushViewController(ClubPrivacyViewController(), animated: true)
            break
        case .account:
            
            UIApplication.topController()?.navigationController?.pushViewController(ClubMyselfInfoViewController(), animated: true)
            break
        case .policy:
            
            let urlString = RaeRequestParameterConfig.shared.baseUrlStr + "ct=zhanwu&ac=agreement&id=2&language=0"
            let webView = ClubWebViewController()
            webView.urlString = urlString
            webView.titleName = "隐私协议"
            UIApplication.topController()?.navigationController?.pushViewController(webView, animated: true)
            break
        case .terms:
            
            let urlString = RaeRequestParameterConfig.shared.baseUrlStr + "ct=zhanwu&ac=agreement&id=1&language=0"
            let webView = ClubWebViewController()
            webView.urlString = urlString
            webView.titleName = "用户协议"
            UIApplication.topController()?.navigationController?.pushViewController(webView, animated: true)
            break
        case .alert:
            
            UIApplication.topController()?.navigationController?.pushViewController(ClubAlertSettingViewController(), animated: true)
            break
        default:
            return
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ClubSettingItemCell: UITableViewCell {
    
    private let iconImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(15.font_medium).end()
    private let rightImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_settings_next").end()

    var model:ClubSettingModel?{
        didSet{
            guard let md = model else { return }
            iconImageV.image = md.icon.toImage
            titleLabel.text = md.title
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageV
            titleLabel
            rightImageV
        }
        
        iconImageV.leading(12.0.Scale).size(24.0.Scale).centerVertically()
        rightImageV.trailing(12.0.Scale).size(20.0.Scale).centerVertically()
        titleLabel.leading(48.0.Scale).trailing(48.0.Scale)
        titleLabel.CenterY == iconImageV.CenterY
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ClubSettingOutCell: UITableViewCell {
    
    private var bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(8.0.Scale).end()
    private let iconImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(15.font_medium).end()
    private let rightImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_settings_next").end()
    private var codeImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_settings_QRCode").end()
    private let codeLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(12.font_semiBold)
        .text("扫码添加客服").alignment(.center).end()

    var model:ClubSettingModel?{
        didSet{
            guard let md = model else { return }
            iconImageV.image = md.icon.toImage
            titleLabel.text = md.title
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            bgView.subviews{
                iconImageV
                titleLabel
                rightImageV
            }
            codeImageView
            codeLabel
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(0).height(64.0.Scale)
        iconImageV.leading(12.0.Scale).size(24.0.Scale).centerVertically()
        rightImageV.trailing(12.0.Scale).size(20.0.Scale).centerVertically()
        titleLabel.leading(48.0.Scale).trailing(48.0.Scale)
        titleLabel.CenterY == iconImageV.CenterY
        codeImageView.size(145.0.Scale).centerHorizontally()
        codeImageView.Top == bgView.Bottom + 22.0.Scale
        codeLabel.centerHorizontally()
        codeLabel.Top == codeImageView.Bottom + 5.0.Scale
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


//MARK: 77游戏乐园
class ClubGame77ClubCell: UITableViewCell, RaeLottieAnimationProtocol{
    
    private var bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(8.0.Scale).end()
    private let logoImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_settings_logo").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#44474D").font(18.font_semiBold)
        .text("乐玩").end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#44474D").font(12.font_semiBold)
        .text("VICE INFO").end()
    private let versionLabel = c_make_ex(UILabel.self)
        .bgColor("#44474D").color("#F2F3F5").font(9.font_semiBold)
        .alignment(.center).end()
    private let linkImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_settings_link").end()
    private let serviceImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_settings_service").end()
    private let timeImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_settings_time").end()
    private let linkLabel = c_make_ex(UILabel.self)
        .text("官方网站").color("#44474D").font(13.font_semiBold).end()
    private let linkSubLabel = c_make_ex(UILabel.self)
        .text("https://www.lwan.com").color("#44474D").font(11.font_semiBold).end()
    private let serviceLabel = c_make_ex(UILabel.self)
        .text("联系客服").color("#44474D").font(13.font_semiBold).end()
    private let serviceSubLabel = c_make_ex(UILabel.self)
        .text("028-65221233").color("#44474D").font(11.font_semiBold).end()
    private let workLabel = c_make_ex(UILabel.self)
        .text("工作时间").color("#44474D").font(13.font_semiBold).end()
    private let workSubLabel = c_make_ex(UILabel.self)
        .text("周一至周五  9:00-18：00").color("#44474D").font(11.font_semiBold).end()
    private let qqButton = c_make_ex(UIButton.self)
        .image("Mine_settings_QQ").end()
    private let bottomLabel = c_make_ex(UILabel.self)
        .color("#AEB0B3").font(12.font_medium).alignment(.center).end()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            bgView.subviews{
                logoImageV
                titleLabel
                subLabel
                versionLabel
                linkImageV
                serviceImageV
                timeImageV
                linkLabel
                linkSubLabel
                serviceLabel
                serviceSubLabel
                workLabel
                workSubLabel
                qqButton
                bottomLabel
            }
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(0).bottom(12.0.Scale)
        logoImageV.leading(16.0.Scale).top(16.0.Scale).width(60.0.Scale).height(54.0.Scale)
        titleLabel.leading(88.0.Scale).top(27.0.Scale)
        versionLabel.width(40.0.Scale).height(14.0.Scale)
        versionLabel.CenterY == titleLabel.CenterY
        versionLabel.Leading == titleLabel.Trailing + 4.0.Scale
        subLabel.leading(88.0.Scale).top(49.0.Scale)
        linkImageV.leading(40.0.Scale).size(24.0.Scale)
        linkImageV.Top == logoImageV.Bottom + 15.0.Scale
        serviceImageV.leading(40.0.Scale).size(24.0.Scale)
        serviceImageV.Top == linkImageV.Bottom + 30.0.Scale
        timeImageV.leading(40.0.Scale).size(24.0.Scale)
        timeImageV.Top == serviceImageV.Bottom + 30.0.Scale
        linkLabel.leading(88.0.Scale)
        linkLabel.Top == subLabel.Bottom + 17.0.Scale
        linkSubLabel.leading(88.0.Scale)
        linkSubLabel.Top == linkLabel.Bottom + 2.0.Scale
        serviceLabel.leading(88.0.Scale)
        serviceLabel.Top == linkSubLabel.Bottom + 20.0.Scale
        serviceSubLabel.leading(88.0.Scale)
        serviceSubLabel.Top == serviceLabel.Bottom + 2.0.Scale
        workLabel.leading(88.0.Scale)
        workLabel.Top == serviceSubLabel.Bottom + 20.0.Scale
        workSubLabel.leading(88.0.Scale)
        workSubLabel.Top == workLabel.Bottom + 2.0.Scale
        qqButton.trailing(16.0.Scale).bottom(50.0.Scale).size(72.0.Scale)
        bottomLabel.leading(20.0.Scale).trailing(20.0.Scale).bottom(12.0.Scale)
        
        let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
        versionLabel.text = "v" + (version ?? "")
        let versionString = "version_" + (version ?? "")
        bottomLabel.text = "乐玩 | " + versionString
        bgView.isUserInteractionEnabled = true
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        versionLabel.rae.corner(8.0, corners: [.layerMinXMinYCorner,.layerMaxXMaxYCorner])
        versionLabel.rae.corner(2.0, corners: [.layerMaxXMinYCorner,.layerMinXMaxYCorner])
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
